package com.saturn.titan.web.controller;

import java.text.ParseException;
import java.util.Date;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.mapper.JsonMapper;

import com.saturn.titan.core.bean.Node;
import com.saturn.titan.core.bean.User;
import com.saturn.titan.core.service.AccountService;
import com.saturn.titan.core.service.CollectedCommentService;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/collector")
public class CollectorController {

	private static Logger logger = LoggerFactory.getLogger(CollectorController.class);
	
	@Autowired
	private CollectedCommentService collectedCommentService;
	
	@Autowired
	private AccountService accountService;

	private static JsonMapper mapperNoEmpty = JsonMapper.nonEmptyMapper();

	@RequestMapping(value = "testjsp", method = RequestMethod.GET)
	public String testjsp(Model model) throws ParseException {
		return "test/test";
	}
	
	
	@RequestMapping(value = "nodeManager", method = RequestMethod.GET)
	public String nodeManager(Model model) throws ParseException {
		return "collector/nodemanager";
	}
	
	@RequestMapping(value = "collectorTestWithSeed/{seedid}", method = RequestMethod.GET)
	@ResponseBody
	public String collectorTestWithSeed(@PathVariable("seedid")
	Long seedid, Model model) throws ParseException {
		return seedid+"";
	}
	
	@RequestMapping(value = "proxyIPValid", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public String proxyIPValid(@RequestParam("sProxy") String sProxy, @RequestParam("url") String url) {
		return sProxy;
	}
	
	@RequestMapping(value = "startTest", method = RequestMethod.GET)
	@ResponseBody
	public String startTest(@Valid Node node) throws ParseException {
		String s = "";
		try {
			Node n = collectedCommentService.findNodeById(1L);
			s = mapperNoEmpty.toJson(n);
		} catch (Exception e) {
			e.printStackTrace();
			s = "{\"success\":false,\"message\":\"异常\"}";
		}

		return s;
	}
	
	@RequestMapping(value = "startTestUser", method = RequestMethod.GET)
	@ResponseBody
	public String startTestUser() throws ParseException {
		String s = "";
		try {
			User n = accountService.getUser(1L);
			s = mapperNoEmpty.toJson(n);
		} catch (Exception e) {
			e.printStackTrace();
			s = "{\"success\":false,\"message\":\"异常\"}";
		}

		return s;
	}
	
	@RequestMapping(value = "testInsert", method = RequestMethod.GET)
	@ResponseBody
	public String testInsert(@Valid Node node) throws ParseException {
		String s = "";
		try {
			collectedCommentService.insert(node);
			s = mapperNoEmpty.toJson(node);
		} catch (Exception e) {
			e.printStackTrace();
			s = "{\"success\":false,\"message\":\"异常\"}";
		}
		return s;
	}
	
	@RequestMapping(value = "testLogFile", method = RequestMethod.GET)
	@ResponseBody
	public String testLogFile(@Valid Node node) throws ParseException {
		for(int i = 0 ; i < 1000000 ; i ++){
			logger.debug("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"+i);
		}
		return "OK";
	}
	
	@RequestMapping(value = "testTransactional", method = RequestMethod.GET)
	@ResponseBody
	public String testTransactional(@Valid Node node) throws ParseException {
		String s = "";
		try {
			collectedCommentService.testTransactional(node);
			s = mapperNoEmpty.toJson(node);
		} catch (Exception e) {
			e.printStackTrace();
			s = "{\"success\":false,\"message\":\"本方法测试事务回滚\"}";
		}
		return s;
	}
	
	@RequestMapping(value = "testupdate", method = RequestMethod.GET)
	@ResponseBody
	public String testupdate() throws ParseException {
		String s = "";
		try {
			Node n = collectedCommentService.findNodeById(1L);
			n.setName("abc"+new Date());
			collectedCommentService.testUpdate(n);
			
		} catch (Exception e) {
			e.printStackTrace();
			s = "{\"success\":false,\"message\":\"本方法测试事务回滚\"}";
		}
		return s;
	}
	
}
